package com.ruoyi.web.controller.personnel;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson2.JSON;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.constant.GlobalConstants;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.inspection.constant.InspectionConstant;
import com.ruoyi.system.domain.KeyPersonnelAdmin;
import com.ruoyi.system.domain.KeyPersonnelAdminCount;
import com.ruoyi.system.domain.KeyPersonnelAdminType;
import com.ruoyi.system.service.KeyPersonnelAdminService;
import com.ruoyi.system.service.KeyPersonnelAdminTypeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 两类重点人员(KeyPersonnelAdmin)表控制层
 *
 * @author makejava
 * @since 2022-11-18 09:31:26
 */
@Slf4j
@RestController
@RequestMapping("key/personnel/admin")
public class KeyPersonnelAdminController extends BaseController {

    @Resource
    private KeyPersonnelAdminService keyPersonnelAdminService;
    @Resource
    private KeyPersonnelAdminTypeService keyPersonnelAdminTypeService;

    /**
     * 分页查询
     *
     * @param keyPersonnelAdmin 筛选条件
     * @return 查询结果
     */
    @GetMapping("list")
    public TableDataInfo queryByPage(KeyPersonnelAdmin keyPersonnelAdmin) {
        startPage();
        List<KeyPersonnelAdmin> list = keyPersonnelAdminService.selectList(keyPersonnelAdmin);
        if (!CollectionUtils.isEmpty(list)) {
            for (KeyPersonnelAdmin promise : list) {
                String staffCode = GlobalConstants.COMPANY_CODE + "01" +
                        StrUtil.fill(promise.getId()+"", '0', 7, true);
                promise.setStaffCode(staffCode);
                List<KeyPersonnelAdminType> adminType = keyPersonnelAdminTypeService.queryByPId(promise.getId());
                if (!CollectionUtils.isEmpty(adminType)) {
                    promise.setKeyPersonnelAdminTypeList(adminType);
                }
            }
        }
        return getDataTable(list);
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public ResponseEntity<KeyPersonnelAdmin> queryById(@PathVariable("id") Integer id) {
        KeyPersonnelAdmin person = this.keyPersonnelAdminService.queryById(id);
        String staffCode = GlobalConstants.COMPANY_CODE + "01" +
                StrUtil.fill(person.getId()+"", '0', 7, true);
        person.setStaffCode(staffCode);
        return ResponseEntity.ok(person);
    }

    /**
     * 新增数据
     *
     * @param keyPersonnelAdmin 实体
     * @return 新增结果
     */
    @PostMapping("add")
    //@PreAuthorize("@ss.hasPermi('key:personnel:admin:add')")
    public ResponseEntity<KeyPersonnelAdmin> add(@Validated @RequestBody KeyPersonnelAdmin keyPersonnelAdmin) {

        log.info("新增数据 参数:{}", JSON.toJSONString(keyPersonnelAdmin));

        return ResponseEntity.ok(this.keyPersonnelAdminService.insert(keyPersonnelAdmin));
    }

    /**
     * 编辑数据
     *
     * @param keyPersonnelAdmin 实体
     * @return 编辑结果
     */
    @PutMapping("edit")
  //  @PreAuthorize("@ss.hasPermi('key:personnel:admin:edit')")
    public ResponseEntity<KeyPersonnelAdmin> edit(@Validated @RequestBody KeyPersonnelAdmin keyPersonnelAdmin) {
        return ResponseEntity.ok(this.keyPersonnelAdminService.update(keyPersonnelAdmin));
    }

    /**
     * 人员
     *
     * @return 结果
     */
    @PostMapping("count")
    public ResponseEntity<KeyPersonnelAdminCount> count() {
        KeyPersonnelAdminCount keyPersonnelAdminCount = new KeyPersonnelAdminCount();

        //总人数
        List<KeyPersonnelAdmin> keyPersonnelAdmins = keyPersonnelAdminService.selectList(new KeyPersonnelAdmin());
        keyPersonnelAdminCount.setPersonnelCount(keyPersonnelAdmins.size());

        //专职安全人数
        KeyPersonnelAdminType keyPersonnelAdminOne = new KeyPersonnelAdminType();
        keyPersonnelAdminOne.setPersonnelType("专职安全人员");
        List<KeyPersonnelAdminType> keyPersonnelAdminTypeListOne = keyPersonnelAdminTypeService.queryByPage(keyPersonnelAdminOne);
        keyPersonnelAdminCount.setSoleCount(keyPersonnelAdminTypeListOne.size());

        //注册安全工程师人数
        KeyPersonnelAdminType keyPersonnelAdminTypeTwo = new KeyPersonnelAdminType();
        keyPersonnelAdminTypeTwo.setPersonnelType("注册安全工程师");
        List<KeyPersonnelAdminType> keyPersonnelAdminTypeListTwo = keyPersonnelAdminTypeService.queryByPage(keyPersonnelAdminTypeTwo);
        keyPersonnelAdminCount.setLogonCount(keyPersonnelAdminTypeListTwo.size());

        return ResponseEntity.ok(keyPersonnelAdminCount);
    }

    /**
     * 删除数据
     *
     * @param keyPersonnelAdmin 主键
     * @return 删除是否成功
     */
    @DeleteMapping("delete")
    //@PreAuthorize("@ss.hasPermi('key:personnel:admin:delete')")
    public ResponseEntity<Boolean> deleteById(@Validated @RequestBody KeyPersonnelAdmin keyPersonnelAdmin) {
        return ResponseEntity.ok(this.keyPersonnelAdminService.deleteById(keyPersonnelAdmin.getId()));
    }

    /**
     * 导入
     * @param file
     * @param updateSupport
     * @return
     * @throws Exception
     */
    @Log(title = "两类重点人员", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
   // @PreAuthorize("@ss.hasPermi('key:personnel:admin:importData')")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception {
        ExcelUtil<KeyPersonnelAdmin> util = new ExcelUtil<>(KeyPersonnelAdmin.class);
        List<KeyPersonnelAdmin> keyPersonnelAdmins = util.importExcel(file.getInputStream());
        String operName = getUsername();
        String message = keyPersonnelAdminService.importExcel(keyPersonnelAdmins, updateSupport, operName);
        return success(message);
    }

    /**
     * 导出
     * @param response
     * @param keyPersonnelAdmin
     */
    @Log(title = "两类重点人员", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
   // @PreAuthorize("@ss.hasPermi('key:personnel:admin:export')")
    public void export(HttpServletResponse response, KeyPersonnelAdmin keyPersonnelAdmin) {
        List<KeyPersonnelAdmin> list = keyPersonnelAdminService.selectList(keyPersonnelAdmin);
        ExcelUtil<KeyPersonnelAdmin> util = new ExcelUtil<>(KeyPersonnelAdmin.class);
        util.exportExcel(response, list, "两类重点人员");
    }

    /**
     * 导入模板
     * @param response
     */
    @PostMapping("/importTemplate")
    //@PreAuthorize("@ss.hasPermi('key:personnel:admin:importTemplate')")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<KeyPersonnelAdmin> util = new ExcelUtil<>(KeyPersonnelAdmin.class);
        util.importTemplateExcel(response, "两类重点人员");
    }

}

